package com.example.board.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.board.mapper.PublicFilterMapper;
import com.example.board.pojo.vo.PublicFilterVo;
import com.example.board.service.PublicFilterService;
import com.example.board.util.DateQuery;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class PublicFilterServiceImpl implements PublicFilterService {

   private final PublicFilterMapper publicFilterMapper;

    @Override
    public List<PublicFilterVo> getSalesMan() {
        //三个月人员入职
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startDate = LocalDateTime.parse(DateQuery.getMothEndLast2(), df);
        return publicFilterMapper.findSalesMan(startDate);
    }

    @Override
    public JSONArray getRegionalFiltering() {
        //数据库带有父子结构

        //返回格式
        JSONArray js = new JSONArray();
//1. 查库-列表如原型图所示
        List<PublicFilterVo> screenGuidanceList = publicFilterMapper.findAllCityTeam();


//分组保留原有顺序-(按BrandName分组，并保留其原有顺序)

        Map<String, List<PublicFilterVo>> lists = screenGuidanceList.stream().collect(Collectors.groupingBy(PublicFilterVo::getLabel, LinkedHashMap::new, Collectors.toCollection(ArrayList::new)));

//遍历
        for (Map.Entry<String, List<PublicFilterVo>> entry : lists.entrySet()) {
            JSONObject brandObject = new JSONObject();
//            //获取list集合
            List<PublicFilterVo> value = entry.getValue();
//            //获取key值
            String keys = entry.getKey();

            brandObject.put("label", keys);
            brandObject.put("value", keys);
//
            JSONArray modelArray = new JSONArray();

            //Map<String, List<ScreenGuidanceVO>> modelList = value.stream().collect(Collectors.groupingBy(ScreenGuidanceVO::getModelName));--未保证其顺序
            //保持原有顺序-（LinkedHashMap按插曲顺序排序）
            Map<String, List<PublicFilterVo>> modelList = value.stream().collect(Collectors.groupingBy(PublicFilterVo::getValue, LinkedHashMap::new, Collectors.toCollection(ArrayList::new)));

            for (Map.Entry<String, List<PublicFilterVo>> modelEntry : modelList.entrySet()) {
                JSONArray modelArray1 = new JSONArray();
                JSONObject modelObject = new JSONObject();
                //获取list集合
                List<PublicFilterVo> modelValue = modelEntry.getValue();

                //获取key值
                String modelKey = modelEntry.getKey();

//                modelObject.put("label", modelKey);
//                //获取中下游点字段的集合

                modelObject.put("value", modelKey);
                modelObject.put("label", modelKey);
                modelArray.add(modelObject);


//                List<String> downNo = modelValue.stream().map(Region::getTeam).distinct().collect(Collectors.toList());
                Map<String, List<PublicFilterVo>> downNo = modelValue.stream().collect(Collectors.groupingBy(PublicFilterVo::getTeam, LinkedHashMap::new, Collectors.toCollection(ArrayList::new)));

                for (Map.Entry<String, List<PublicFilterVo>> stringListEntry : downNo.entrySet()) {
                    JSONObject downNoObject = new JSONObject();
                    //获取list集合
                    List<PublicFilterVo> downNoValue = stringListEntry.getValue();

                    //获取key值
                    String downNoKey = stringListEntry.getKey();

                    System.out.println("key:");
                    for (int i = 0; i < downNoValue.size(); i++) {
                        System.out.println(downNoValue.get(i));
                    }
//                modelObject.put("label", modelKey);
//                //获取中下游点字段的集合

                    downNoObject.put("value", downNoKey);
                    downNoObject.put("label", downNoKey);
                    modelArray1.add(downNoObject);

                }
                modelObject.put("children", modelArray1);
            }
            brandObject.put("children", modelArray);


            js.add(brandObject);

        }
        System.out.println(js);

        return js;
    }


}
